
(function(){
	if(typeof(Pls) === "undefined"){
		Pls = {};
	}
	//标签
	if (typeof(Pls.TAG) === "undefined") {
		Pls.TAG = {};
	}
	//脚本 （array）缓存已加载过的脚本
	if (typeof(Pls.TAG.SCRIPTS) === "undefined") {
		Pls.TAG.SCRIPTS = [];
	}
	//样式 （array）缓存已加载过的样式
	if (typeof(Pls.TAG.CSS) === "undefined") {
		Pls.TAG.CSS = [];
	}
})();

Pls.TAG.COMMON = {
		storeBeforeload:function(store,operation,eOpts){
	  		 Ext.apply(store.proxy.extraParams,store.getExtraParams());  
		},
		treeStoreBeforeload:function(store,operation,eOpts){
	  		if( operation.node && operation.node.data.id ){
		  		var condition = new Condition("node",operation.node.data.id);
		  		store.addCondition(condition);
	  		}
	  		Ext.apply(store.proxy.extraParams,store.getExtraParams());  
		},
		appendStyles:function(path, styles){
			styles = styles || "";
			if(styles == ""){
				return;
			}
			var _styles = styles.split(",");
			if(document.createStyleSheet){
				for(var i = 0, len = _styles.length; i < len; i++){
					if (this._checkCSS(_styles[i])) {
						document.createStyleSheet(_styles[i]);
					}
				}
			}
			else {
				var link,
					frag = document.createDocumentFragment();
				var htmlHead = document.getElementsByTagName("head")[0];
				for(var i = 0, len = _styles.length; i < len; i++){
					if (this._checkCSS(_styles[i])) {
						link = document.createElement("link");
						link.setAttribute("type", "text/css");
						link.setAttribute("rel", "stylesheet");
						link.setAttribute("href", _styles[i]);
						frag.appendChild(link);
					}
				}
				htmlHead.appendChild(frag);
			}
		},
		/**
		 * 检查样式是否已引入
		 * @param {Object} url
		 */
		_checkCSS: function(url){
			var a = document,
				css = Pls.TAG.CSS,
				lowerCaseUrl = url.toLowerCase();
			for (var i = css.length - 1; i >= 0; i--) {
				if (css[i] == url) {
					return false;
				}
			}
			//如果不是css文件，则不加载该文件
			if (lowerCaseUrl.indexOf(".css") == -1) {
				return false;
			}
			Pls.TAG.CSS.push(url);
			return true;
		},
		/**
		 * 检查脚本是否已经引入
		 * @param {Object} url
		 */
		_checkJs: function(url){
			var a = document,
				sc = Pls.TAG.SCRIPTS,
				lowerCaseUrl = url.toLowerCase();
			for (var i = sc.length - 1; i >= 0; i--) {
				if (sc[i] == url) {
					return false;
				}
			}
			//如果不是js文件，则不加载该文件
			if (lowerCaseUrl.indexOf(".js") == -1) {
				return false;
			}
			Pls.TAG.SCRIPTS.push(url);
			return true;
		},
		/**
		 * 检查是否外链资源
		 * @param {Object} str
		 */
		_checkIsLink: function(str){
			if (str.indexOf("http://") >= 0) {
				return true;
			}
			return false;
		},
		/**
		 * 阻塞式加载
		 * @param {Object} path
		 * @param {Object} scripts
		 */
		loadJs: function(path, scripts){
			if (!scripts || scripts == "") {
				return;
			}
			var _scripts = scripts.split(",");
			for (var i = 0, len = _scripts.length; i < len; i++) {
				if (!this._checkJs(_scripts[i])) {
					continue;
				}
				document.write("<sc" +"ript type='text/javascript' src='" +
					_scripts[i] + "'></s" + "cript>");
			}
		},
		/**
		 * 异步加载
		 * @param {Object} path
		 * @param {Object} scripts
		 */
		lazyLoadJs: function(path, scripts){
			if (!scripts || scripts == "") {
				return;
			}
			var doc = document,
				_scripts = scripts.split(","),
				me = this;
			setTimeout(function(){
				var frag = doc.createDocumentFragment(), 
					s = doc.createElement('script');
					s.type = 'text/javascript';
				if (!Ext.isIE) {
					s.async = true;
				}
				for (var i = 0, len = _scripts.length; i < len; i++) {
					if (!me._checkJs(_scripts[i])) {
						continue;
					}
					s = s.cloneNode(true);
					s.src = _scripts[i];
					frag.appendChild(s);
				}
				doc.body.appendChild(frag);
			}, 0);
		}
};
